import java.util.Iterator;

public class TextFileIterator implements Iterator<String> {
	
	private String text; 		
	private String nextWord;
	
	public TextFileIterator(Resource res) {
		text = getAsString(res);
		text = text.replaceAll("-\n(?=[a-z])", "");
		nextWord = extractNextWord();
	}
	
	public boolean hasNext() {
		return nextWord != null;
	}
	
	public String next() {
		String tmp = nextWord;
		nextWord = extractNextWord();
		return tmp;
	}
	
	public void remove() {
		throw new UnsupportedOperationException();
	}
	
	private String getAsString(Resource res) { 
		return ("Viel Erfolg in dieser Klausur ;-)");
	} 
	
	private String extractNextWord() {
		int beginIdx = 0;
		while (beginIdx < text.length() &&
				!Character.isLetter(text.charAt(beginIdx)) &&
				!Character.isDigit(text.charAt(beginIdx))) { 
			beginIdx++;
		} 
		
		int idx = beginIdx;
		
		while (idx < text.length() &&
				(Character.isLetter(text.charAt(idx)) ||
						Character.isDigit(text.charAt(idx)))) { 
			idx++;
		}
		
		if (beginIdx == idx) {
			return null;
		}

		String word = text.substring(beginIdx, idx); 
		text = text.substring(idx); 
		return word;
	}
}
